import { Component, OnInit } from '@angular/core';
import {CompanyService} from '../../service/company.service';
import {Position} from '../../entity/position';
import {ActivatedRoute} from '@angular/router';

@Component({
  selector: 'app-find-all-positions',
  templateUrl: './find-all-positions.component.html',
  styleUrls: ['./find-all-positions.component.css']
})
export class FindAllPositionsComponent implements OnInit {
  name = '';
  searchName: string;
  searchFlag = false;
  positions: any ;
  // pageable: any = {
  //   page: 0,
  //   // size: 7,
  //   // 默认分页数据为7条
  //   // sort: 'DESC'
  //   // 默认为倒序
  // };
  thisPage: Number;
  totalPages: Number;
  nextPage: Number = 0;
  arr = [1, 2, 3, 4, 5];
  constructor(
    private companyService: CompanyService,
    private route: ActivatedRoute
  ) {
    this.nextPage = Number.parseInt(this.route.snapshot.paramMap.get('page'));
    this.loadData();
  }

  ngOnInit() {
  }

  loadData() {
    if (this.searchFlag) {
      this.companyService.findAllLike({page: this.nextPage, name: this.searchName})
        .subscribe((page: any) => {
          this.positions = page.content;
          this.thisPage = page.pageable.pageNumber;
          this.totalPages = page.totalPages;
        });
    } else {
      this.companyService.findAll({page: this.nextPage})
        .subscribe((page: any) => {
          this.positions = page.content;
          this.thisPage = page.pageable.pageNumber;
          this.totalPages = page.totalPages;
        });
    }
  }
  goToPage(page: number) {
    if (page != this.thisPage && page >= 0 && page < this.totalPages) {
      this.nextPage = page;
      this.loadData();
    }
  }
  search() {
    if (this.name == '') {
      this.searchFlag = false;
    } else {
      this.searchFlag = true;
    }
    this.searchName = this.name;
    this.nextPage = 0;
    this.loadData();
  }
}
